package com.cuitrip.business.home.recommend;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.cuitrip.business.search.SearchActivity;
import com.cuitrip.business.setting.SettingActivity;
import com.cuitrip.service.R;
import com.lab.app.BaseFragment;
import com.lab.jumper.Jumper;
import com.lab.widget.CTViewPager;
import com.lab.widget.pager.TabPageIndicator;
import com.ogaclejapan.smarttablayout.SmartTabLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RecommendFragment extends BaseFragment {

    @Bind(R.id.ct_view_pager)
    CTViewPager mViewPager;
    @Bind(R.id.viewpagertab)
    SmartTabLayout mViewpagertab;
    private View mContentView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mContentView == null) {
            mContentView = inflater.inflate(R.layout.ct_page_recommend, container, false);
        } else {
            ViewParent parent = mContentView.getParent();
            if (parent != null) {
                ((ViewGroup) parent).removeView(mContentView);
            }
        }
        ButterKnife.bind(this, mContentView);
        initView();
        return mContentView;
    }

    private void initView() {
        RecommendPagerAdapter mMainViewPagerAdapter = new RecommendPagerAdapter(getOptimizedFragmentManager());
        mViewPager.setAdapter(mMainViewPagerAdapter);
        mViewPager.requestDisallowInterceptTouchEvent(true);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setCanScroll(true);
        mViewPager.setCurrentItem(0, false);
        mViewpagertab.setViewPager(mViewPager);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.search_icon)
    public void onClick() {
        Jumper.launchActivity(getActivity(), new Intent(getActivity(), SearchActivity.class));
    }

//    @OnClick(R.id.app_icon)
//    public void onClickAppIcon() {
//        if (getHostActivity() != null) {
//            String url = SettingActivity.MARKET_URL + getHostActivity().getPackageName();
//            Intent installIntent = new Intent("android.intent.action.VIEW");
//            installIntent.setData(Uri.parse(url));
//            Intent chooserIntent = Intent.createChooser(installIntent, getString(R.string.RATE_CONTENT));
//            Jumper.launchActivity(getHostActivity(), chooserIntent);
//        }
//    }


    public class RecommendPagerAdapter extends FragmentPagerAdapter implements TabPageIndicator.ITabView {

        private String[] pageTitles = new String[]{};


        public RecommendPagerAdapter(FragmentManager fm) {
            super(fm);
            pageTitles = getResources().getStringArray(R.array.recommend_pager);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return pageTitles[position];
        }


        @Override
        public int getCount() {
            int count = pageTitles.length;
            return count;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new RecommendCardListFragment();

                case 1:
                    return new RecommendCategoryFragment();


            }
            return null;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
        }

        @Override
        public int getIndex() {
            return 0;
        }

        @Override
        public void setIndex(int index) {

        }
    }

}
